<template>
  <el-breadcrumb class="app-breadcrumb" separator="/">
    <transition-group name="breadcrumb">
      <el-breadcrumb-item v-for="(item,index) in levelList" v-if="item" :key="index">
        <span class="no-redirect">{{ item }}</span>
      </el-breadcrumb-item>
    </transition-group>
  </el-breadcrumb>
</template>

<script>
import { mapGetters } from 'vuex'
export default {
  data() {
    return {
      levelList: []
    }
  },
  computed: {
    ...mapGetters([
      'menus'
    ])
  },
  watch: {
    $route() {
      this.getBreadcrumb()
    }
  },
  created() {
    this.getBreadcrumb()
  },
  methods: {
    getBreadcrumb() {
      this.getMenusItem(this.$route.path)
    },
    getMenusItem(path) { // 最多三层
      const length = this.menus.length
      for (let i = 0; i < length; i++) { // 第一层
        if (this.menus[i].href === path) {
          this.levelList = [this.menus[i].text]
          return
        } else if (this.menus[i].nodes && this.menus[i].nodes.length > 0) {
          for (let j = 0; j < this.menus[i].nodes.length; j++) { // 第二层
            if (this.menus[i].nodes[j].href === path) {
              this.levelList = [this.menus[i].text, this.menus[i].nodes[j].text]
              return
            } else if (this.menus[i].nodes[j].nodes && this.menus[i].nodes[j].nodes.length > 0) {
              for (let z = 0; z < this.menus[i].nodes[j].nodes.length; z++) { // 第三层
                if (this.menus[i].nodes[j].nodes[z].href === path) {
                  this.levelList = [this.menus[i].text, this.menus[i].nodes[j].text, this.menus[i].nodes[j].nodes[z].text]
                  return
                }
              }
            }
          }
        }
      }
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
  .app-breadcrumb.el-breadcrumb {
    display: inline-block;
    font-size: 14px;
    line-height: 50px;
    margin-left: 10px;
    .no-redirect {
      color: #97a8be;
      cursor: text;
    }
  }
</style>
